package com.baizhi.dao.impl;

import com.baizhi.dao.EmpDAO;
import com.baizhi.entity.Emp;
import com.baizhi.entity.User;
import com.baizhi.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class EmpDAOImpl implements EmpDAO {
    @Override
    public List<Emp> selectAllEmps() {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Emp> emps = new ArrayList<Emp>();
        try{
            conn = JDBCUtil.getConnection();
            String sql = "select * from t_emp";
            pstm = conn.prepareStatement(sql);
            rs = pstm.executeQuery();
            while(rs.next()){
                Emp emp = new Emp();
                emp.setId(rs.getInt(1));
                emp.setName(rs.getString(2));
                emp.setSalary(rs.getDouble(3));
                emp.setBirthday(rs.getDate(4));
                emps.add(emp);
            }
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }finally {
            JDBCUtil.close(conn,pstm,rs);
        }
        return emps;
    }

    @Override
    public void deleteEmpById(int id) {
        Connection conn = null;
        PreparedStatement pstm = null;
        try{
            conn = JDBCUtil.getConnection();
            String sql = "delete from t_emp where id=?";
            pstm = conn.prepareStatement(sql);
            pstm.setInt(1,id);
            pstm.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.close(pstm);
        }
    }

    @Override
    public void insertEmp(Emp emp) {
        Connection conn = null;
        PreparedStatement pstm = null;
        try{
            conn = JDBCUtil.getConnection();
            String sql = "insert into t_emp values(null,?,?,?)";
            pstm = conn.prepareStatement(sql);
            pstm.setString(1,emp.getName());
            pstm.setDouble(2,emp.getSalary());
            pstm.setDate(3,new java.sql.Date(emp.getBirthday().getTime()));
            pstm.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.close(pstm);
        }
    }

    @Override
    public Emp selectById(int id) {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        Emp emp = null;
        try{
            conn = JDBCUtil.getConnection();
            String sql = "select * from t_emp where id=?";
            pstm = conn.prepareStatement(sql);
            pstm.setInt(1,id);
            rs = pstm.executeQuery();
            if(rs.next()){
                emp = new Emp();
                emp.setId(rs.getInt(1));
                emp.setName(rs.getString(2));
                emp.setSalary(rs.getDouble(3));
                emp.setBirthday(rs.getDate(4));
            }
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }finally {
            JDBCUtil.close(conn,pstm,rs);
        }
        return emp;
    }

    @Override
    public void updateEmp(Emp emp) {
        Connection conn = null;
        PreparedStatement pstm = null;
        try{
            conn = JDBCUtil.getConnection();
            String sql = "update t_emp set name=? , salary=? , birthday=? where id=?";
            pstm = conn.prepareStatement(sql);
            pstm.setString(1,emp.getName());
            pstm.setDouble(2,emp.getSalary());
            pstm.setDate(3,new java.sql.Date(emp.getBirthday().getTime()));
            pstm.setInt(4,emp.getId());
            pstm.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtil.close(pstm);
        }
    }
}
